var app = getApp();
// var d = require("../../../../../../template/calendar/calendar.js");
Page({


  /**
   * 页面的初始数据
   */
  data: {
    nesARR: [],
    active: true,
    showCalendar: false,
    year: 0,
    month: 0,
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    time: '',
    selectVal: '',
    typeindex: false,
    proIndex: 0,
    proshow: false,
    cityname: '',
    money: 0,
    count: 0,
    total: 0,
    arr: [],
    addPackageList: [],
    listcity: [],
    tcProject: [],
    addproject: []
  },

  calenderOpen: function () {
    this.setData({
      showCalendar: !this.data.showCalendar
    })
  },
  closeMask: function () {
    this.setData({
      showCalendar: !this.data.showCalendar
    })
  },
  select(e) {
    console.log(e)
    var that = this
    app.util.request({
      'url': 'entry/wxapp/tijian.timemumenone',
      data: {
        chooseDate: e.detail
      },
      success: function (res) {
        console.log(res)
        if (res.data == "") {
          that.setData({
            typeindex: true,
            week: res.data.week,
            listcity: []
          })
        } else {
          that.setData({
            typeindex: true,
            week: res.data.week,
            listcity: res.data.data
          })
        }
      }
    })
    that.setData({
      selectVal: e.detail,
      showCalendar: !this.data.showCalendar
    })
    var selectVal = this.data.selectVal
    var time = selectVal.substring(5, selectVal.length)
    that.setData({
      time: time
    })
    console.log(time)
  },
  goToCheckItemsDetail: function () {
    var that = this
    var detail = JSON.stringify(that.data.detail)
    var tcProject = JSON.stringify(that.data.tcProject)
    var tid = that.data.tid
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/tijian/mealCont/mealCont?detail=' + detail + '&tcProject=' + tcProject + '&tid=' + tid,
    })
  },
  showInfo: function (e) {
    var name = e.currentTarget.dataset.itemName
    var cont = e.currentTarget.dataset.itemIntro
    console.log(e)
    wx.showModal({
      title: name,
      content: cont,
      showCancel: false,
      confirmText: '确定',
      confirmColor: '#fd965b',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getCalendarData: function (e) {

  },
  //选择
  handleTapView: function (e) {
    var index = e.currentTarget.dataset.index
    var addPackageList = this.data.addPackageList; //通用加项包
    addPackageList[index].checked = !addPackageList[index].checked
    var zmoney = this.data.total //低价
    var count = this.data.count
    var addproject = []
    console.log(addPackageList)
    console.log(zmoney)
    console.log(count)
    for (var i = 0; i < addPackageList.length; i++) {
      if (addPackageList[i].checked) {
        addproject.push(addPackageList[i])
      }
    }
    for (var i = 0; i < addproject.length; i++) {
      zmoney = zmoney + addproject[i].price
    }
    this.setData({
      zmoney: zmoney,
      addproject: addproject
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    var bm = JSON.parse(options.bm)
    var nesARR = that.data.nesARR
    var is_fenxiao = options.is_fenxiao
    var hid = JSON.parse(options.hid)
    for (var i = 0; i < bm.length; i++) {
      nesARR.push(bm[i].city)
    }
    that.setData({
      nesARR: nesARR,
      is_fenxiao: is_fenxiao,
      hid: hid
    })
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    console.log(options)
    var tcProject = options.project;
    console.log(tcProject)
    var that = this
    let now = new Date();
    var detail = JSON.parse(decodeURIComponent(options.detail))

    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    var count = options.count
    var money = options.money
    var title = options.title

    var city = wx.getStorageSync('city')

    that.setData({
      year: year,
      month: month,
      today: now.getDate(),
      isToday: '' + year + month + now.getDate(),
      title: title,
      money: parseFloat(money),
      cityname: city,
      detail: detail,
      bm: bm,
      cityid: bm[0].city.parentid,
      
    })
    console.log(that.data.today)
    var tid = options.tid
    app.util.request({
      'url':"entry/wxapp/tijian.project",
      data:{
        id:tcProject
      },
      success:function(res)
      {
        that.setData({
          tcProject: res.data
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/tijian.detail',
      data: {
        id: tid,
        hid: hid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          detail: res.data,
          count: res.data.t_msg.length,
          addPackageList: res.data.t_msg_fujia
        })
      }
    })
    that.setData({
      tid: tid
    })

    //请求 后三天的 时间 周
    app.util.request({
      'url': 'entry/wxapp/tijian.timelist',
      data: {
        q: 1
      },
      success: function (res) {
        console.log(res)
        var num = 0
        var list_wedate = res.data
        var time_in = list_wedate[0]['date']
        for (var i = 0; i < list_wedate.length; i++) {
          var date = list_wedate[i].date
          var time = date.substring(5, date.length)
          list_wedate[i].date = time
          if ([i] == 0) {
            list_wedate[i].active = true
            list_wedate[i].num = 0
          } else {
            list_wedate[i].active = false
            list_wedate[i].num = 0
          }
        }
        that.setData({
          list_wedate: list_wedate,
          time_in: time_in
        })
        var num = 0;
        var list_wedate = that.data.list_wedate
        for (var i = 1; i < list_wedate.length; i++) {
          num = num + 100
          list_wedate[i].num = num
        }
        that.setData({
          list_wedate: list_wedate
        })
        var date = list_wedate[0].date
        var time = list_wedate[0].time
        var chooseDate = time + '-' + date
        console.log(chooseDate)
        app.util.request({
          'url': 'entry/wxapp/tijian.timemumen',
          data: {
            chooseDate: chooseDate,
            cityid: bm[0].city.parentid,
            hid: hid
          },
          success: function (res) {
            console.log(res)
            if (res.data == "") {
              that.setData({
                listcity: [],
                chooseDate: chooseDate
              })
            } else {
              that.setData({
                listcity: res.data.message,
                chooseDate: chooseDate
              })
            }

          }
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    console.log(that.data.listcity)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.cityname) {
      console.log(currPage.data.id)
      var index = that.data.index
      app.util.request({
        'url': 'entry/wxapp/tijian.timelist',
        data: {
          q: index
        },
        success: function (res) {
          console.log(res)
          var num = 0
          var list_wedate = res.data
          var time_in = list_wedate[0]['date']
          for (var i = 0; i < list_wedate.length; i++) {
            var date = list_wedate[i].date
            var time = date.substring(5, date.length)
            list_wedate[i].date = time
            if ([i] == 0) {
              list_wedate[i].active = true
              list_wedate[i].num = 0
            } else {
              list_wedate[i].active = false
              list_wedate[i].num = 0
            }
          }
          that.setData({
            list_wedate: list_wedate,
            time_in: time_in
          })
          var num = 0;
          var list_wedate = that.data.list_wedate
          for (var i = 1; i < list_wedate.length; i++) {
            num = num + 100
            list_wedate[i].num = num
          }
          that.setData({
            list_wedate: list_wedate
          })
          app.util.request({
            'url': 'entry/wxapp/tijian.timemumen',
            data: {
              chooseDate: that.data.chooseDate,
              cityid: currPage.data.id,
              hid: that.data.hid
            },
            success: function (res) {
              console.log(res)
              if (res.data == "") {
                that.setData({
                  listcity: [],
                  chooseDate: that.data.chooseDate,
                  cityid: currPage.data.id
                })
              } else {
                that.setData({
                  listcity: res.data.message,
                  chooseDate: that.data.chooseDate,
                  cityid: currPage.data.id
                })
              }

            }
          })
        }
      })
      // this.setData({
      //   cityname: currPage.data.cityname,
      //   id: currPage.data.id
      // })
    }
    if (currPage.data.names) {
      console.log(currPage.data)
      this.setData({
        names: currPage.data.names,
        tel: currPage.data.tel,
        numcard: currPage.data.numcard,
        sex: currPage.data.sex,
        j_id: currPage.data.j_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  changeDate(e) {
    console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index
    var list_wedate = that.data.list_wedate
    var chooseDate = e.currentTarget.dataset.chooseDate
    var cityid = e.currentTarget.dataset.cityid
    var hid = that.data.hid
    for (var i = 0; i < list_wedate.length; i++) {
      if ([i] == index) {
        list_wedate[i].active = true
      } else {
        list_wedate[i].active = false
      }
    }
    that.setData({
      list_wedate: list_wedate
    })
    app.util.request({
      'url': 'entry/wxapp/tijian.timemumen',
      data: {
        chooseDate: chooseDate,
        cityid: e.currentTarget.dataset.cityid,
        hid: hid
      },
      success: function (res) {
        console.log(res)
        if (res.data == '') {
          that.setData({
            listcity: [],
            time_in: chooseDate,
            index: index
          })
        } else {
          that.setData({
            listcity: res.data.message,
            time_in: chooseDate,
            index: index
          })
        }
      }
    })
  },
  changeDateweek() {
    var that = this
    //请求 后三天的 时间 周
    app.util.request({
      'url': 'entry/wxapp/tijian.timelist',
      data: {
        q: 1
      },
      success: function (res) {
        var num = 0
        var list_wedate = res.data
        for (var i = 0; i < list_wedate.length; i++) {
          var date = list_wedate[i].date
          var time = date.substring(5, date.length)
          list_wedate[i].date = time
          if ([i] == 0) {
            list_wedate[i].active = true
            list_wedate[i].num = 0
          } else {
            list_wedate[i].active = false
            list_wedate[i].num = 0
          }
        }
        that.setData({
          list_wedate: list_wedate
        })
        var num = 0;
        var list_wedate = that.data.list_wedate
        for (var i = 1; i < list_wedate.length; i++) {
          num = num + 100
          list_wedate[i].num = num
        }
        that.setData({
          list_wedate: list_wedate
        })
        var date = list_wedate[0].date
        var time = list_wedate[0].time
        var chooseDate = time + '-' + date
        var cityid = that.data.cityid
        console.log(cityid)
        app.util.request({
          'url': 'entry/wxapp/tijian.timemumen',
          data: {
            chooseDate: chooseDate,
            cityid: cityid
          },
          success: function (res) {
            console.log(res)
            if (res.data == '') {
              that.setData({
                listcity: [],
                typeindex: false

              })
            } else {
              that.setData({
                listcity: res.data.message,
                typeindex: false

              })
            }
          }
        })
      }
    })
  },
  chooseCityTab() {
    var that = this
    var nesARR = JSON.stringify(that.data.nesARR)
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/tijian/choose-city/choose-city?nesARR=' + nesARR,
    })
  },
  goAddon(e) {
    var money = this.data.money
    var chooseDate = this.data.chooseDate
    var time_in = this.data.time_in
    var agentname = e.currentTarget.dataset.agentname
    var bm_id = e.currentTarget.dataset.hid //部门id
    this.setData({
      proIndex: 1,
      proshow: !this.data.proshow,
      total: this.data.money,
      zmoney: this.data.money,
      agentname: agentname,
      time_in: time_in,
      bm_id: bm_id
    })
  },
  // 返回上一步
  goPre: function () {
    // var arr = this.data.arr
    var addPackageList = this.data.addPackageList
    var addproject = this.data.addproject
    var arr = []
    for (var i = 0; i < addPackageList.length; i++) {
      addPackageList[i].checked = false
    }
    var count = this.data.count;
    count = parseInt(count) - parseInt(addproject.length)
    addproject = []
    this.setData({
      proIndex: 0,
      addPackageList: addPackageList,
      count: count,
      addproject: addproject,
      arr: arr
    })
  },
  // 跳转到立即支付
  goNext: function () {
    var arr = this.data.arr
    var tcProject = this.data.tcProject
    var addPackageList = this.data.addPackageList
    var zmoney = this.data.zmoney //总价
    var addproject = this.data.addproject //选中的加项包
    console.log(addproject)
    var list = {
      itemName: this.data.title,
      itemprice: parseFloat(this.data.money),
      tcProject: tcProject,
    }
    console.log(list)
    arr.push(list)
    console.log(arr)
    this.setData({
      proIndex: 2,
      arr: arr
    })
  },
  chooseContact() {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?tijianper=1',
    })
  },
  // 立即支付
  nowpay(e) {

    var that = this
    var is_fenxiao = that.data.is_fenxiao
    var mytkid = app.globalData.mytkid
    var tkid = app.globalData.tkid
    var arr = that.data.arr //套餐数据
    var content = arr[0].tcProject
    var addproject = this.data.addproject //加包项
    var money = parseFloat(e.currentTarget.dataset.money)
    var bm_id = that.data.bm_id
    var hid = that.data.hid
    var uniacid = app.siteInfo.uniacid
    var tid = that.data.tid
    console.log(tid)
    var names = this.data.names
    var tel = this.data.tel
    var numcard = this.data.numcard
    var sex = this.data.sex
    var content_id = "";
    for(var i=0;i<content.length;i++)
    {
      content_id += content[i].id+','
    }
    var addproject_id = "";
    for(var i=0;i<addproject.length;i++)
    {
      addproject_id += addproject[i].id+','
    }
    if (!names && !sex && !tel && !numcard) {
      wx.showToast({
        title: '请选择体检人',
        icon: "none"
      })
      return
    }
   
    //支付
    wx.showModal({
      content: '是否支付',
      success: function (res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/tijian.addtijian',
            data: {
              content_id: content_id,
              addproject_id: addproject_id,
              money: money,
              hid: hid,
              bm_id: bm_id,
              ifpay: 0,
              yy_time: that.data.time_in,
              tid: tid,
              openid: wx.getStorageSync('openid'),
              
              j_id: that.data.j_id
            },
            success: function (res) {
              console.log(res)
              var orders = res.data.ordernums
              if(money != '0' && money != '0.00' && money != '' && money != 0)
              {
                
                app.util.request({
                  'url': 'entry/wxapp/tijian.paytijianorder',
                  header: {
                    "Content-Type": "application/xml"
                  },
                  method: "GET",
                  data: {
                    openid: wx.getStorageSync('openid'),
                    z_tw_money: money,
                    orders: orders
                  },
                  success: function (res) {
                    console.log(res);
                    wx.requestPayment({
                      'timeStamp': res.data.timeStamp,
                      'nonceStr': res.data.nonceStr, //字符串随机数
                      'package': res.data.package,
                      'signType': res.data.signType,
                      'paySign': res.data.paySign,
                      success: function (res) {
                        //查询是否开启分销
                        if (is_fenxiao == '1') {
                          //产生分销
                          app.util.request({
                            'url': 'entry/wxapp/tuike.uptijiantkmoney',
                            data: {
                              tid: tid,
                              tkid: tkid,
                              mytkid: mytkid,
                              money: money,
                              openid: wx.getStorageSync('openid')
                            },
                            success: function (res) {
                              console.log(res)
                            }
                          })
                        }
  
                        //跳转订单页
                        //提醒专家/患者
                        app.util.request({
                          'url': 'entry/wxapp/tijian.update_order',
                          data: {
                            orders: orders,
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                        app.util.request({
                          'url': 'entry/wxapp/tijian.tijmsgmobel',
                          data: {
                            yy_time: that.data.time_in,
                            tid: tid,
                            openid: wx.getStorageSync('openid'),
                            j_id: that.data.j_id,
                            bm_id: bm_id
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                        app.util.request({
                          'url': 'entry/wxapp/tijian.tiwxtemplet',
                          data: {
                            yy_time: that.data.time_in,
                            tid: tid,
                            openid: wx.getStorageSync('openid'),
                            j_id: that.data.j_id,
                            bm_id: bm_id
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
  
                        app.util.request({
                          'url': 'entry/wxapp/tijian.usermsgmobel',
                          data: {
                            yy_time: that.data.time_in,
                            tid: tid,
                            openid: wx.getStorageSync('openid'),
                            j_id: that.data.j_id,
                            bm_id: bm_id
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                        //短信提醒机构管理员
                        app.util.request({
                          'url': 'entry/wxapp/tijian.useryuyuemsg',
                          data: {
                            yy_time: that.data.time_in,
                            tid: tid,
                            openid: wx.getStorageSync('openid'),
                            j_id: that.data.j_id,
                            bm_id: bm_id
                          },
                          success: function (res) {
                            console.log(res)
                          }
                        })
                        wx.showToast({
                          title: '支付成功',
                          icon: 'none',
                          duration: 1500,
                          success: function (res) {
                            wx.redirectTo({
                              url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                            })
                          }
                        })
                      },
                      fail: function () {
                        //支付失败
                        wx.redirectTo({
                          url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                        })
                      }
                    })
                  }
                })
              }else{
                //查询是否开启分销
                if (is_fenxiao == '1') {
                  //产生分销
                  app.util.request({
                    'url': 'entry/wxapp/tuike.uptijiantkmoney',
                    data: {
                      tid: tid,
                      tkid: tkid,
                      mytkid: mytkid,
                      money: money,
                      openid: wx.getStorageSync('openid')
                    },
                    success: function (res) {
                      console.log(res)
                    }
                  })
                }
                app.util.request({
                  'url': 'entry/wxapp/tijian.update_order',
                  data: {
                    orders: orders,
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                //跳转订单页
                //提醒专家/患者

                app.util.request({
                  'url': 'entry/wxapp/tijian.tijmsgmobel',
                  data: {
                    yy_time: that.data.time_in,
                    tid: tid,
                    openid: wx.getStorageSync('openid'),
                    j_id: that.data.j_id,
                    bm_id: bm_id
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                app.util.request({
                  'url': 'entry/wxapp/tijian.tiwxtemplet',
                  data: {
                    yy_time: that.data.time_in,
                    tid: tid,
                    openid: wx.getStorageSync('openid'),
                    j_id: that.data.j_id,
                    bm_id: bm_id
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })

                app.util.request({
                  'url': 'entry/wxapp/tijian.usermsgmobel',
                  data: {
                    yy_time: that.data.time_in,
                    tid: tid,
                    openid: wx.getStorageSync('openid'),
                    j_id: that.data.j_id,
                    bm_id: bm_id
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                //短信提醒机构管理员
                app.util.request({
                  'url': 'entry/wxapp/tijian.useryuyuemsg',
                  data: {
                    yy_time: that.data.time_in,
                    tid: tid,
                    openid: wx.getStorageSync('openid'),
                    j_id: that.data.j_id,
                    bm_id: bm_id
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                wx.showToast({
                  title: '支付成功',
                  icon: 'none',
                  duration: 1500,
                  success: function (res) {
                    wx.redirectTo({
                      url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                    })
                  }
                })
              }
              
            }
          })

        } else {
          app.util.request({
            'url': 'entry/wxapp/tijian.addtijian',
            data: {
              content_id: content_id,
              addproject_id: addproject_id,
              money: money,
              hid: hid,
              bm_id: bm_id,
              ifpay: 0,
              yy_time: that.data.time_in,
              tid: tid,
              openid: wx.getStorageSync('openid'),
              j_id: that.data.j_id
            },
            success: function (res) {
              console.log(res)
              app.util.request({
                'url': 'entry/wxapp/tijian.tijmsgmobel',
                data: {
                  yy_time: that.data.time_in,
                  tid: tid,
                  openid: wx.getStorageSync('openid'),
                  j_id: that.data.j_id,
                  bm_id: bm_id
                },
                success: function (res) {
                  console.log(res)
                }
              })
              app.util.request({
                'url': 'entry/wxapp/tijian.usermsgmobel',
                data: {
                  yy_time: that.data.time_in,
                  tid: tid,
                  openid: wx.getStorageSync('openid'),
                  j_id: that.data.j_id,
                  bm_id: bm_id
                },
                success: function (res) {
                  console.log(res)
                }
              })
              app.util.request({
                'url': 'entry/wxapp/tijian.msgtzadmin',
                data: {
                  yy_time: that.data.time_in,
                  tid: tid,
                  openid: wx.getStorageSync('openid'),
                  j_id: that.data.j_id,
                  bm_id: bm_id
                },
                success: function (res) {
                  console.log(res)
                }
              })
              wx.redirectTo({
                url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
              })
            }
          })
        }
      }

    })


  }

  // 第三部分返回上一步
  // nowback:function(){
  //   this.setData({
  //     proIndex:1,
  //   })
  // }
})